﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Entidades;
using ControlesPadroes;
using CamadaAcessoDadosPostgresql;


namespace InterfaceWinForm
{
    public partial class MenuSistemaForm : WindowForm
    {
               
        public MenuSistemaForm(string pTag, EnumOpenForm pOpenform)
        {
            InitializeComponent();

            CarregarCampos(pTag, pOpenform);
        }



        public MenuSistema CriarMenuSistema(string pTag)
        {
            MenuSistema menu = null;
            try
            {
                menu = new MenuSistemaWork().RetornaMenu(int.Parse(pTag));
            }
            catch(Exception ex)
            {
                MessageBox.Show("Erro ao criar objeto " + ex.Message );
                this.Close();
            }

            return menu;
        }

        private void CarregarCampos(string pTag, EnumOpenForm pOpenform)
        { 
            switch(pOpenform)
            {

                case EnumOpenForm.Incluir:
                    {
                        if (pTag.Length > 0)
                        {
                            radTextBoxIDMenuPai.Text = pTag;
                            radTextBoxIDMenuPai.Enabled = false;
                        }
                        radTextBoxIDMenu.Text = "0";
                        radTextBoxIDMenu.Enabled = false;
                        break;

                    }
                default:
                    {
                        MenuSistema menusistema = CriarMenuSistema(pTag);
                        radTextBoxDescricao.Text = menusistema.Descricao;
                        radTextBoxExclusivo.Text = menusistema.Exclusivo.ToString();
                        radTextBoxIDMenu.Text = menusistema.Idmenu.ToString();
                        radTextBoxIDMenuPai.Text = menusistema.Idmenupai.ToString();
                        radTextBoxObjeto.Text = menusistema.Objeto;
                        radTextBoxOrdem.Text = menusistema.Ordem.ToString();
                        radTextBoxPadrao.Text = menusistema.Padrao.ToString();
                        radTextBoxProjeto.Text = menusistema.Projeto;
                            

                        break;
                    }



            }

            

        }

        private void radButtonFechar_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void radButtonConfirmar_Click(object sender, EventArgs e)
        {
            DescarregarCampos();

        }

        private void DescarregarCampos()
        {
            MenuSistema menusistema = new MenuSistema( int.Parse(radTextBoxIDMenu.Text),
                                                       int.Parse(radTextBoxIDMenuPai.Text),
                                                       int.Parse(radTextBoxOrdem.Text),
                                                       int.Parse(radTextBoxPadrao.Text),
                                                       radTextBoxDescricao.Text,
                                                       radTextBoxObjeto.Text,
                                                       radTextBoxProjeto.Text, 
                                                       int.Parse(radTextBoxExclusivo.Text));

            new MenuSistemaWork().AtualizaMenu(menusistema);

                            
        }
    }
}

